package cc.lj.internal.coupons.domain.ticket.service.impl;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketPackageBatchDO;
import cc.lj.internal.coupons.domain.ticket.model.vo.convert.TicketPackageBatchPOConvertDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketPackageBatchRepository;
import cc.lj.internal.coupons.domain.ticket.service.ITicketPackageBatchDomainService;
import cc.lj.internal.coupons.infrastructure.po.TicketPackageBatchPO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 券包与批次关系领域服务service实现类
 * @author liangxifeng
 * @date 2023-08-24
 */
@Service("ticketPackageBatchDomainServiceImpl")
@Slf4j
public class TicketPackageBatchDomainServiceImpl implements ITicketPackageBatchDomainService {
    //电子券批次表仓储接口
    @Resource(name = "ticketPackageBatchRepository")
    private ITicketPackageBatchRepository repository;
    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    public PageBean<TicketPackageBatchDO> selectPage(PageParam<TicketPackageBatchDO> pageParam) {
        PageInfo<TicketPackageBatchPO> pageInfo = repository.selectPage(pageConvert(pageParam));

        List<TicketPackageBatchDO> list = new ArrayList();
        if(pageInfo.getList().size() > 0)
        {
            for (TicketPackageBatchPO TicketPackageBatchPO : pageInfo.getList()) {
                TicketPackageBatchDO activityDO = TicketPackageBatchPOConvertDO.INSTANCE.convert(TicketPackageBatchPO);
                list.add(activityDO);
            }
        }
        //list的po转换为do后再组装PageBean将其返回上一层
        PageBean pageBean = PageBean.createPageBean(pageInfo.getPageNum(),
                pageInfo.getPageSize(), pageInfo.getTotal(), list);
        return pageBean;
    }


    /**
     * 分页DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<TicketPackageBatchPO> pageConvert(PageParam<TicketPackageBatchDO> pageParam) {
        PageParam<TicketPackageBatchPO> pageParamR = new PageParam<TicketPackageBatchPO>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        TicketPackageBatchPO activityPO = TicketPackageBatchPOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(activityPO);
        return pageParamR;
    }

    /**
     * 通过主键查询一条记录
     * @param id
     * @return
     */
    public TicketPackageBatchDO getById(Integer id)
    {
        TicketPackageBatchPO TicketPackageBatchPO = repository.getById(id);
        return TicketPackageBatchPOConvertDO.INSTANCE.convert(TicketPackageBatchPO);
    }

    /**
     * 根据id修改
     * @param ticketPackageBatchDO
     * @return
     */
    public Integer updateById(TicketPackageBatchDO ticketPackageBatchDO) {
        //TicketPackageBatchPO mainPO = repository.getById(TicketPackageBatchDO.getId());
        TicketPackageBatchPO TicketPackageBatchPO = TicketPackageBatchPOConvertDO.INSTANCE.convert(ticketPackageBatchDO);
        return repository.updateById(TicketPackageBatchPO);
    }

    /**
     * 标准批量获取数据的方法
     * @param ticketPackageBatchDO
     * @return
     */
    public List<TicketPackageBatchDO> getList(TicketPackageBatchDO ticketPackageBatchDO)
    {
        TicketPackageBatchPO TicketPackageBatchPO = TicketPackageBatchPOConvertDO.INSTANCE.convert(ticketPackageBatchDO);
        List<TicketPackageBatchPO> list = repository.getList(TicketPackageBatchPO);
        List<TicketPackageBatchDO> newList = new ArrayList<>();
        for(TicketPackageBatchPO item : list)
        {
            newList.add(TicketPackageBatchPOConvertDO.INSTANCE.convert(item));
        }
        return newList;
    }

}
